const Models = require('./../../models')

class Cars{
    static async addCar(ctx){
        const req = ctx.request.body;
        if (req.name && req.carNumber && req.price && req.carAvater && req.carmanagerId){
            const data = await Models.cars.create({
                name: req.name,
                isUse: req.isUse,
                carNumber: req.carNumber,
                price: req.price,
                carAvater: req.carAvater,
                carmanagerId: req.carmanagerId,
                createdAt: new Date(),
                updatedAt: new Date()
            });
            ctx.response.status = 200;
            ctx.response.body = {
                code : 200,
                msg: '数据添加成功',
                data
            }
        }else{
            ctx.response.status = 200;
            ctx.response.body = {
                code: 401,
                msg: '请求数据不完整或者数据格式错误'
            }
        }
    }

    static async getCar(ctx){
        const data = await Models.cars.findAll();
        ctx.response.status = 200;
        ctx.response.body = {
            code : 200,
            msg: '数据请求成功',
            data
        }
    }

    static async editCar(ctx){
        const req = ctx.request.body;
        const {id,name,carNumber,price,carAvater,carmanagerId,isUse} = req;
        const data = await Models.cars.update({id,name,carNumber,price,carAvater,carmanagerId,isUse},{
            where:{
                id
            }
        })
        ctx.response.status = 200;
        ctx.response.body = {
            code: 200,
            msg: '数据编辑成功',
            data
        }
    }

    static async deleteCar(ctx){
        const req = ctx.request.body;
        await Models.cars.update({isUse: !req.isUse},{
            where:{
                id: req.id
            }
        })
        ctx.response.status = 200;
        ctx.response.body = {
            code: 200,
            msg: '数据请求成功'
        }
    }

    static async getUsedCar(ctx){
        const req = ctx.request.body;
        const data = await Models.cars.findAll({
            where: {
                isUse: true
            }
        })
        ctx.response.status = 200;
        ctx.response.body = {
            code: 200,
            msg: '数据过去成功',
            data
        }
    }

    static async getPrice(ctx){
        const req = ctx.request.body;
        const data = await Models.cars.findOne({
            where: {
                id: req.carId
            }
        })
        ctx.response.status = 200;
        ctx.response.body = {
            code: 200,
            msg: '价格获取成功',
            data
        }
    }
}

module.exports = Cars;